package org.devocean.groupbuying.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.dao.ProductDAO;
import org.devocean.groupbuying.domain.ArticlesLocalization;
import org.devocean.groupbuying.domain.Product;
import org.devocean.groupbuying.service.ArticlesService;
import org.devocean.groupbuying.service.ExploredService;
import org.devocean.groupbuying.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDAO productDAO;

	@Autowired
	private ArticlesService articlesService;

	@Autowired
	private ExploredService exploredService;

	@Transactional
	public Product getProductById(Integer id) {
		return productDAO.findProductById(id);
	}

	@Transactional
	public Product addOrUpdateProduct(Product product) {

		Product newProduct = null;
		if (product.getProductID() == null) {
			newProduct = productDAO.saveProduct(product);
		} else {
			newProduct = productDAO.updateProduct(product);
		}

		// exploredService.addOrUpdateExplored(newProduct);
		return newProduct;

	}

	@Transactional
	public void removeProduct(Integer id) {
		ArticlesLocalization al = articlesService.getArticleById(id)
				.getArticlesLocalization();
		System.out.println("AL : " + al.getArticlesLocalizationID());

//		exploredService.deleteExplored(exploredService.getExploredByProduct(id)
//				.getExploredID());

		if (al.getArticles().size() > 1) {
			articlesService.removeArticle(id);
		} else {
			Product product = this.getProductByArticleLocalization(al
					.getArticlesLocalizationID());
			productDAO.removeProduct(product.getProductID());
		}

	}

	public long countProducts() {

		return productDAO.countProducts();
	}

	public List<Product> getAllProducts() {
		return productDAO.findAllProducts();
	}

	public List<Product> getProductsEntries(int start, int size) {

		return productDAO.findProductsEntries(start, size);
	}

	public Product getProductByArticleLocalization(Integer articleLocalizationID) {

		return productDAO
				.findProductByArticleLocalization(articleLocalizationID);
	}

	public Product getProductByArticleID(Integer articleID) {

		return productDAO.findProductByArticle(articleID);
	}

	public Product getProductByLang(Integer localizationKey,
			String localizationRequest) {
		Product product = productDAO.findProductByLang(localizationKey,
				localizationRequest);
		if (product != null) {
			return product;
		} else {

			Product newProduct = productDAO
					.findProductByArticleLocalization(localizationKey);
			newProduct.getArticlesLocalization().setArticles(null);
			return newProduct;
		}

	}

	@Transactional
	public Product getProductByProductID(Integer productID,
			String localizationRequest) {
		Product product = this.getProductById(productID);

		return this.getProductByLang(product.getArticlesLocalization()
				.getArticlesLocalizationID(), localizationRequest);
	}

	public Map<String, Object> search(String searchText,
			ConcurrentHashMap<String, Object> searchResult) {
		return searchResult;
	}

}
